package com.common.log;

import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommomUtil {

	private static final char PACKAGE_SEPARATOR_CHAR = '.';
	private static Logger logger = LoggerFactory.getLogger(CommomUtil.class);
	private static final boolean IS_WINDOWS = isWindows0();
	public static String DateFormat() {
		Date d = new Date();
		System.out.println(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		String dateNowStr = sdf.format(d);
		return dateNowStr;
	}
	
	public static boolean isWindows() {
        return IS_WINDOWS;
    }
	
	private static boolean isWindows0() {
        boolean windows = CommomUtil.get("os.name", "").toLowerCase(Locale.US).contains("win");
        if (windows) {
               logger.debug("Platform: Windows");
        }
        return windows;
    }
	
	public static String get(final String key, String def) {
		if (key == null) {
			throw new NullPointerException("key");
		}
		if (key.isEmpty()) {
			throw new IllegalArgumentException("key must not be empty.");
		}
		String value = null;
		try {
			if (System.getSecurityManager() == null) {
				value = System.getProperty(key);
			} else {
				value = AccessController.doPrivileged(new PrivilegedAction<String>() {
					@Override
					public String run() {
						return System.getProperty(key);
					}
				});
			}
		} catch (Exception e) {
			logger.warn("Unable to retrieve a system property '{}'; default values will be used.", key, e);
		}

		if (value == null) {
			return def;
		}

		return value;
	}
	public static String DateFormat1() {
		Date d = new Date();
		System.out.println(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateNowStr = sdf.format(d);
		return dateNowStr;
	}

	public static String swapCase(String str) {
		if (null == str || str.isEmpty()) {
			return str;
		}
		char[] buffer = str.toCharArray();
		for (int i = 0; i < buffer.length; i++) {
			char ch = buffer[i];
			if (Character.isUpperCase(ch)) {
				// buffer[i] = Character.toLowerCase(ch);
			} else if (Character.isTitleCase(ch)) {
				// buffer[i] = Character.toLowerCase(ch);
			} else if (Character.isLowerCase(ch)) {
				buffer[i] = Character.toUpperCase(ch);
			}
		}
		return new String(buffer);
	}


	public static String simpleClassName(Object o) {
        if (o == null) {
            return "null_object";
        } else {
            return simpleClassName(o.getClass());
        }
    }
	
	public static String simpleClassName(Class<?> clazz) {
        String className = checkNotNull(clazz, "clazz").getName();
        final int lastDotIdx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
        if (lastDotIdx > -1) {
            return className.substring(lastDotIdx + 1);
        }
        return className;
    }
	
	public static <T> T checkNotNull(T arg, String text) {
        if (arg == null) {
            throw new NullPointerException(text);
        }
        return arg;
    }
	
	public static <T> T[] checkNonEmpty(T[] array, String name) {
        checkNotNull(array, name);
        checkPositive(array.length, name + ".length");
        return array;
    }
	
	public static int checkPositive(int i, String name) {
        if (i <= 0) {
            throw new IllegalArgumentException(name + ": " + i + " (expected: > 0)");
        }
        return i;
    }
	
	public static void main1(String[] args) throws IOException {
	}
}
